var BT;
var page = {
    ZOOM: BT.context('zoom'),
    LAT: BT.context('latitude'),
    LNG: BT.context('longitude'),
    init: function () {
        this.loadGoogleScript();
    },
    loadGoogleScript: function () {
        var script = document.createElement('script');
        script.type = 'text/javascript';
        script.src = MAP_URL + '&callback=page.loadMap';
        document.body.appendChild(script);
    },
    loadMap: function(){
        var mapOptions = {
            zoom: page.ZOOM,
            center: new google.maps.LatLng(page.LAT, page.LNG),
            streetViewControl: false,
            mapTypeControl: false,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        page.map = new google.maps.Map(document.getElementById('map'), mapOptions);
        
        // 去除google地图自带poi层
        page.map.setOptions({
            styles: [{
                        'featureType': 'poi',
                        'stylers': [{ 
                            'visibility': 'off'
                        }]
                    }]
        });
        page.placeMarker(page.map, page.LAT, page.LNG);
    },
    setCenter: function (map, lat, lng) {
        if(!google) {
            return;
        }
        map.setCenter(new google.maps.LatLng(lat, lng));
    },
    placeMarker: function(map, lat, lng) {
        if(google) {
            if (page.marker) {
                page.marker.setMap(null);
            }

            page.marker = page.createMarker({
                draggable: false,
                animation: google.maps.Animation.DROP,
                position: new google.maps.LatLng(lat, lng),
                map: page.map,
                zIndex: google.maps.Marker.MAX_ZINDEX
            });
        }
    },
    createMarker: function(opts) {
        if(!google) {
            return;
        }
        if(!opts.icon) {
            opts.icon = 'http://photos.breadtrip.com/covers_2014_07_31_4ba6bcaf386a99ade58c34c09d6d26ed.png';
        }
        var marker = new google.maps.Marker(opts);
        return marker;
    },
};

$(document).ready(function() {
    page.init();
});